package com.atguigu.gmall.filter;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.WebFilter;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/7/7 10:21
 * @Version 1.0
 */
@Configuration
@Component
public class CorsConfig {

    @Bean
    public WebFilter webFilter(){
        //  创建 CorsConfiguration 对象
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 设置允许访问的域名
        corsConfiguration.addAllowedMethod("*"); // 设置允许访问的方法
        corsConfiguration.addAllowedHeader("*"); // 设置允许请求头
        corsConfiguration.setAllowCredentials(true); // 设置允许携带cookie
        //  创建CorsConfigurationSource 实现类对象
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(urlBasedCorsConfigurationSource);
    }
}
